<template>
  <div class="app-container">
    <!-- 查询 -->
    <el-form  :inline="true" :model="query" size="mini"  class="demo-form-inline">
      
      <el-form-item label="角色名称：">
        <el-input v-model="query.name" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-permission="'role:search'" icon='el-icon-search' type="primary" @click="queryData">查询</el-button>
        <el-button icon='el-icon-refresh' @click="reload">重置</el-button>
        <el-button v-permission="'role:add'" icon="el-icon-circle-plus-outline" type="primary" @click="openAdd" v-if="!roleIds">新增</el-button>
        <el-button icon="el-icon-circle-plus-outline" type="success" @click="handleUserRole" v-if="roleIds">设置角色</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="list"
      border
      stripe
      style="width: 100%"
      ref="dataTable"
      row-key="id"
      @selection-change="handleSelectionChange">
      <el-table-column
      type="selection"
      width="55"
      v-if="roleIds"
      align="center">
      </el-table-column>
      <el-table-column
        align="center"
        type=index
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column 
        align="center"
        prop="name"
        label="角色名称"
        >
      </el-table-column>
      <el-table-column
        align="center"
        prop="remark"
        label="备注"
        >
      </el-table-column>
      
      <el-table-column  
        align="center"   
        label="操作"
        width="340" v-if="!roleIds">
        <template slot-scope="scope">
          <el-button
            size="mini"   
            type="primary"
            v-permission="'role:permission'"      
            @click="handlePermission(scope.row.id)">分配权限</el-button>
          <el-button
            size="mini"
            type="success"
            v-permission="'role:edit'"
            @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            v-permission="'role:delete'"
            @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页功能 -->   
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total">
    </el-pagination>
    <!-- 弹出窗口 -->
    <edit :visible="edit.visible" :formData="edit.formData" :remoteClose="remoteClose" :title="edit.title" ></edit>
    <permission :visible="per.visible" :remoteClose="remotePerClose" title="分配权限" :roleId="per.roleId"></permission>
  </div>
</template>

<script>
import api from '@/api/role'
import Edit from './edit.vue'
import Permission from './permission'
export default {
  name:'Role',
  components: { Edit , Permission},
  props:{
    roleIds: null
  },
  data() {
    return {
      list:[],
      page:{ // 分页相关
        total:0, // 总记录数
        size:20,// 每页显示20条数据
        current:1// 当前页码
      },
      query:{},// 查询条件
      edit:{
        visible:false,
        formData:{
          
        },
        title:''
      },
      per:{
        roleId: null, // 角色
        visible: false
      },
      // 选中的对象
      checkedRoleList:[]
    }
  },
  watch:{
    roleIds(){
      this.query={} // 清空查询框数据
      this.queryData() // 重新获取数据
    }
  },
  methods: {
    // 请求数据
    fetchData(){
      api.getList(this.query,this.current,this.size).then(
        responce=>{
          this.list=responce.data.records
          this.page.total=responce.data.total
          this.checkedRoles()
        }
      )
    },
    // 勾选用户角色
    checkedRoles(){
      this.$refs.dataTable.clearSelection();
      if(this.roleIds){
        this.list.forEach(item => {
          if(this.roleIds.indexOf(item.id)!==-1){
            this.$refs.dataTable.toggleRowSelection(item,true);
          }
        });
      }
    },
    //分配权限
    handlePermission(id){
      this.per.visible=true
      this.per.roleId=id
    },
    // 编辑
    handleEdit(id){
      api.getById(id).then((response)=>{
          
          if (response.code === 20000){
            this.edit.formData=response.data
            this.edit.title = '编辑'
            this.edit.visible = true
            
          }
        })
    },
    // 删除
    handleDelete(id) {
      this.$confirm('确认删除这条记录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          api.deleteById(id).then((response)=>{
              this.$message({
              type: response.code===20000?'success':'error',
              message: response.message
            });
          })
          this.fetchData()
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //修改个数
    handleSizeChange(val) {       
        this.page.size=val
        this.fetchData()
    },

    // 修改当前页
    handleCurrentChange(val) {      
      this.page.current=val
      this.fetchData()
    },
    // 查询功能
    queryData() {
        this.page.current=1
        this.fetchData()
    },
    // 重载
    reload(){
      this.query={}
      this.fetchData()
    },
    // 新增
    openAdd(){
      this.edit.visible=true
      this.edit.title='新增'
    },
    // 触发关闭弹出的新增修改子组件窗口
    remoteClose(){
      this.edit.visible=false
      this.edit.formData={}
      this.fetchData()

    },
    // 删除分配权限窗口
    remotePerClose(){
      this.per.visible=false
      this.per.roleId=null
      this.fetchData()

    },
    // 改变选择触发
    handleSelectionChange(val){
      this.checkedRoleList = val
    },
    // 设置角色
    handleUserRole(){
      const checkedRoleIds = [];
      this.checkedRoleList.forEach(item=>{
        checkedRoleIds.push(item.id)
      })
      this.$emit('saveUserRole',checkedRoleIds)
    }
  },
  created() {
    this.fetchData()
  },

}
</script>

<style>

</style>